<?php


namespace App\Shuixing\Controllers;

use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Models\WxCircle;
use App\Models\WxUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class IndexController
{
    public function index()
    {

        View()->composer('shuixing/home', function ($view) {
            $view->with(['menu' => 0]);
        });

        //社区贡献者
        $userdata = WxUser::whereIn('id', [10000, 10001, 9999, 10188])
            ->get(['id', 'user_name', 'user_avatar', 'user_introduce', 'is_member', 'is_official', 'is_authentication']);

        //推荐圈子
        $tcdata = WxCircle::where('circle_state', 1)
            ->where('is_top_recommend', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'asc')->limit(3)->get();
        //最火圈子
        $hcdata = WxCircle::where('circle_state', 1)
            ->where('is_hot', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'asc')->limit(3)->get();
        //最新圈子
        $xcdata = WxCircle::where('circle_state', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('id', 'desc')->limit(3)->get();

        //轮播帖子
        $lpdata = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->where('is_information', 1)
            ->orderBy('id', 'desc')->get();
        $lpdata = self::postsParame($lpdata);//文章详情

        //4篇推荐帖子
        $tpdata = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->whereIn('user_id', [10000,9999])
            ->orderBy('id', 'desc')->limit(4)->get();
        $tpdata = self::postsParame($tpdata);//文章详情

        return view('shuixing/index', ['tpdata' => $tpdata, 'lpdata' => $lpdata, 'userdata' => $userdata, 'tcdata' => $tcdata, 'hcdata' => $hcdata, 'xcdata' => $xcdata]);
    }

    public function wxLogin(Request $request)
    {
        Cache::put('login_token_wx_pc', $request->token, $seconds = 60);
        Cache::put('login_scene_wx_pc', $request->scene, $seconds = 60);
        return true;
    }

    public function pcLogin()
    {
        $data = [
            'token' => Cache::pull('login_token_wx_pc'),
            'scene' => Cache::pull('login_scene_wx_pc')
        ];
        return $data;
    }

    public static function postsParame($data)
    {
        $data->map(function ($v) {
            $v->user = WxUser::where('id', $v->user_id)
                ->first(['user_avatar', 'user_name', 'id', 'is_member', 'is_official', 'is_authentication']);//发帖人
            $tagsIds = WxPostsTag::where('posts_id', $v->id)->pluck('tags_id');//标签ID列表
            $v->tags = WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//标签列表
            $v->like_count = WxLike::where('posts_id', $v->id)->count(); // 帖子喜欢数量
            $v->comment_count = WxComment::where(['posts_id' => $v->id, 'comment_state' => 1])->count(); // 评论数量
            $v->image = WxPostsImg::where('posts_id', $v->id)->value('img_url'); // 帖子图片
            $v->vimage = WxPostsVideo::where('posts_id', $v->id)->value('video_thumb_url'); // 帖子视频封面
            $v->format_time = format_datetime($v->created_at); // 发帖时间

        });

        return $data;
    }


}
